package it.unisa.services;

import it.unisa.beans.Account;
import it.unisa.beans.Contour;
import it.unisa.dao.AccountDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import it.unisa.beans.Account;
import it.unisa.dao.AccountDao;
import it.unisa.dao.CommentDao;
/**
 * 
 * @author Alfonso
 * TODO aggiungere i metodi di modifica di account (tipo modificare password, email, ecc..)
 */

public class SystemWS {
	public String loginUser(String username,String password) {
		
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		AccountDao acc = (AccountDao)appContext.getBean("accountDao");
		Account a=new Account();
		a.setNickname(username);
		a.setPassword(password);
		List<Account> list=acc.find(a);
		for(Account account:list){
		
			account.setOnline(1);
			acc.update(account);
			return account.getSsn();
		}
		return "";
	}
	
	public boolean updateAccount(Account account){
		return true;
	}
	
	public it.unisa.beanService.Account[] viewActiveUsers(float positionX, float positionY, int round) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		AccountDao acc = (AccountDao)appContext.getBean("accountDao");
		ArrayList<Account> list = (ArrayList<Account>)acc.findAllAccounts();
		
		List<Account> near=new ArrayList<Account>();
		for(int i=0; i<list.size(); i++) {
			if(near(positionX, positionY, list.get(i).getPositionX(), list.get(i).getPositionY(), round)) {
				near.add(list.get(i));
			}
		}
		
		it.unisa.beanService.Account[] nearToReturn=new it.unisa.beanService.Account[near.size()];
		int i=0;
		for(Account a:near){
			it.unisa.beanService.Account account=new it.unisa.beanService.Account();
			account.setSsn(a.getSsn());
			account.setEmail(a.getEmail());
			account.setEnabled(a.getEnabled());
			account.setName(a.getName());
			account.setNickname(a.getNickname());
			account.setPositionX(a.getPositionX());
			account.setPositionY(a.getPositionY());
			account.setRole(a.getRole());
			account.setSurname(a.getSurname());
			nearToReturn[i]=account;
			i++;
		}
		return nearToReturn;
	}
	
	private boolean near(float positionAX, float positionAY,  float positionBX, float positionBY, int round) {
		float ah = positionBX - positionAX;
		float bh = positionBY - positionAY;
		float d = (float) Math.sqrt(ah*ah + bh*bh);
		if(d<=round) return true;
		return false;
	}
	
	public boolean logoutUser(String username) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		AccountDao acc = (AccountDao)appContext.getBean("accountDao");
		Account a=new Account();
		a.setNickname(username);
		List<Account> list=acc.find(a);
		for(Account account:list){
			account.setOnline(0);
			acc.update(account);
			return true;
		}
		return false;
	}
	
	public boolean newBaseAccount(Account account) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		AccountDao acc = (AccountDao)appContext.getBean("accountDao");
		return acc.insert(account);
	}
	
	public boolean removeAccount(Account account){
		return true;
	}

	public boolean promoteArcheologistAccount(Account account) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		AccountDao acc = (AccountDao)appContext.getBean("accountDao");
		account.setRole("archeologyst");
		acc.update(account);
		return true;
	}
	
	public static void main(String[] args){
		SystemWS s=new SystemWS();
		s.loginUser("moretti", "moretti");
	}
	
	
 }
